package cn.lh.basic.service.impl;

import cn.lh.basic.mapper.BaseMapper;
import cn.lh.basic.query.BaseQuery;
import cn.lh.basic.service.IBaseService;
import cn.lh.basic.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired // 这里不能填resource
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void add(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void del(Long id) {
        baseMapper.remove(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T findById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> findAll() {
        return baseMapper.loadAll();
    }



    @Override
    public Page<T> queryPage(BaseQuery query) {
        Integer totals = baseMapper.queryCount(query);
        List<T> ts = baseMapper.queryData(query);
        Page<T> pageList = new Page<>(totals, ts);
        return pageList;
    }

    @Override
    @Transactional
    public void batchDelete(Long[] ids) {
        baseMapper.batchDelete(ids);
    }
}
